from urllib import request, parse

# 获取一个页面内容
'''
with request.urlopen("http://127.0.0.1:8080/CSMS/login;jsessionid=8BEF2D46386D4532B4E0E494DEACC675") as f:
    data=f.read()
    print("Status:",f.status,f.reason)
    for k,v in f.getheaders():
        print("%s: %s" % (k,v))
    print("Data:",data.decode("utf-8"))
'''

# 发送Post请求
'''
print("login to joven system")
username = input("username:")
password = input("password:")
login_data = parse.urlencode([
    ("username", username),
    ("password", password),
    ("jsessionid", "8BEF2D46386D4532B4E0E494DEACC675")
])
req = request.Request("http://127.0.0.1:8080/CSMS/login")
req.add_header('Origin', 'http://127.0.0.1:8080')
req.add_header('User-Agent',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36')
req.add_header('Referer', 'http://127.0.0.1:8080/CSMS/login;jsessionid=8BEF2D46386D4532B4E0E494DEACC675')
with request.urlopen(req, data=login_data.encode('utf-8')) as f:
    print("Status:", f.status, f.reason)
    for k, v in f.getheaders():
        print(k + ": " + v)
    print('Data:',f.read().decode('utf-8'))
'''

#获得百度页面
print("get baidu.com page")
req = request.Request("https://www.baidu.com/img/baidu.svg")
req.add_header('Origin', 'https://www.baidu.com')
req.add_header('User-Agent',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36')
req.add_header('Referer', 'https://www.baidu.com/index.php?tn=monline_3_dg')
with request.urlopen(req) as f:
    print("Status:", f.status, f.reason)
    for k, v in f.getheaders():
        print(k + ": " + v)
    print('\nData:')
    print(f.read().decode('utf-8'))